<?php

namespace app\controller\admin\system\merchant;


use crmeb\basic\BaseController;
use app\common\repositories\system\merchant\MerchantRuleRepository;
use app\validate\admin\MerchantRuleValidate;
use FormBuilder\Exception\FormBuilderException;
use think\App;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class MerchantRule extends BaseController
{
    /**
     * @var MerchantRuleRepository
     */
    protected $repository;

    /**
     * @param App $app
     * @param MerchantRuleRepository $repository
     */
    public function __construct(App $app, MerchantRuleRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    /**
     * @return mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function lst()
    {
        [$page, $limit] = $this->getPage();

        $where = $this->request->params(['title']);

        return app('json')->success($this->repository->search($where, $page, $limit));
    }

    /**
     * @return mixed
     * @throws FormBuilderException
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function createForm()
    {
        return app('json')->success(formToData($this->repository->form(0)));
    }

    /**
     * @param $id
     * @return mixed
     * @throws FormBuilderException
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function updateForm($id)
    {
        if (!$this->repository->merExists(0, $id))
            return app('json')->fail('数据不存在');
        return app('json')->success(formToData($this->repository->updateForm(0, $id)));
    }

    /**
     * @param MerchantRuleValidate $validate
     * @return mixed
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function create(MerchantRuleValidate $validate)
    {
        $data = $this->checkParams($validate);
        $this->repository->create($data);
        return app('json')->success('添加成功');
    }

    /**
     * @param $id
     * @param MerchantRuleValidate $validate
     * @return mixed
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function update($id, MerchantRuleValidate $validate)
    {
        $data = $this->checkParams($validate);
        if (!$this->repository->merExists($id))
            return app('json')->fail('数据不存在');
        $this->repository->update($id, $data);
        return app('json')->success('编辑成功');
    }

    /**
     * @param $id
     * @return mixed
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function switchStatus($id)
    {
        $status = $this->request->param('status', 0) == 1 ? 1 : 0;
        if (!$this->repository->exists($id))
            return app('json')->fail('数据不存在');
        $this->repository->update($id, compact('status'));
        return app('json')->success('修改成功');
    }

    /**
     * @param $id
     * @return mixed
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function delete($id)
    {
        $this->repository->delete($id, 0);
        return app('json')->success('删除成功');
    }


    /**
     * @param $id
     * @return mixed
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function detail($id)
    {
        if (!$this->repository->exists($id))
            return app('json')->fail('数据不存在');
        return app('json')->success($this->repository->get($id,0));
    }

    /**
     * @param MerchantRuleValidate $validate
     * @return mixed
     * @author: fupeng
     * @day: 2023/11/27
     */
    public function checkParams(MerchantRuleValidate $validate)
    {
        $data = $this->request->params([ 'title', 'content', 'status', 'sort']);
        $validate->check($data);
        return $data;
    }

    public function select()
    {
        $result = $this->repository->getFormatList(0,1);
        return app('json')->success($result);
    }
}
